package com.huluobo.a2204kotlinshopping.mine

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.SPUtils
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.databinding.FragmentMineBinding
import com.huluobo.a2204kotlinshopping.setting.SettingActivity
import com.huluobo.a2204kotlinshopping.setting.SettingViewModel
import com.huluobo.a2204kotlinshopping.utils.ImageUtils
import com.huluobo.base.bean.UserBean
import com.huluobo.base.db.AppDBUtils
import com.huluobo.base.net.Const
import com.huluobo.base.view.BaseMVVMFragment

class MineFragment : BaseMVVMFragment<FragmentMineBinding, MineViewModel>() {
    private lateinit var userBean: UserBean
    override fun getLayoutId(): Int = R.layout.fragment_mine
    override fun initData() {
//        AppDBUtils.getDb().userDao.queryUserInfo(SPUtils.getInstance().getString(Const.PARAM_USERNAME))
        viewModel.getUserData(SPUtils.getInstance().getString(Const.PARAM_USERNAME))


        viewModel.getUserDataSuccess.observe(this) {
            ImageUtils.loadImage(requireContext(), it.icon, dataBinding.mineUserIconIv)
            dataBinding.mineUserNicknameTv.text = it.nickname
        }

        viewModel.getUserDataFailed.observe(this) {
            dataBinding.mineUserNicknameTv.text = "登录/注册"
        }

    }

    override fun initView() {
        dataBinding.mineOrdersLl.setOnClickListener {
            val intent = Intent(requireContext(), OrdersActivity::class.java)
            startActivity(intent)
        }

        dataBinding.mineAddressLl.setOnClickListener {
            val intent = Intent(requireContext(), AddressActivity::class.java)
            startActivity(intent)
        }

        dataBinding.mineSettingIv.setOnClickListener {
            val intent = Intent(requireContext(), SettingActivity::class.java)
            startActivity(intent)
        }
    }

    override fun getViewModel(): Class<MineViewModel> {
        return MineViewModel::class.java
    }
}